﻿
Partial Class UCMain_Section_Profile
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsNothing(Session("section")) Then
            Session.Clear()
            Response.Redirect("../Login.aspx")
        End If
        If Not IsPostBack Then
            loadData()
        End If

    End Sub

    Sub loadData()
        Dim sql As String = ""
        ' sql = " select * from user_table where section='MMC'"
        sql = " select *,Switch ([status]='1','ปกติ'"
        sql += " ,[status]='2','ยกเลิกใช้งานชั่วคราว'"
        sql += " ,[status]='3','ลาออก') as sStatus,[status] from user_table "
        sql += " where 1=1 "
        If ddlSection.SelectedValue <> "0" Then
            sql += " and [section] = '" & ddlSection.SelectedItem.Text & "'"
        End If
        ' order by ID_PICOP "
        Dim dt As New Data.DataTable
        dt = clsCommand.SQL_exec(sql)
        If dt.Rows.Count > 0 Then
            GridView1.DataSource = dt
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub ddlSection_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlSection.SelectedIndexChanged
        loadData()
    End Sub

    Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName.ToLower = "bedit" Then
            Response.Redirect("Signup.aspx?ID_USER=" & e.CommandArgument & "")
        ElseIf e.CommandName.ToLower = "bhold" Then
            Dim sql As String = ""
            ' hold = status = 2
            sql = " update user_table set [status]='2' where id_user=" & e.CommandArgument
            Dim err As String = ""
            err = clsCommand.SQL_cmd(sql)
            If err = "" Then
                Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "EditSectionProfile.aspx")
            Else
                img.Visible = True
                lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
            End If
        ElseIf e.CommandName.ToLower = "bdelete" Then
            Dim sql As String = ""
            ' delete = status = 3
            sql = " update user_table set [status]='3' where id_user=" & e.CommandArgument
            Dim err As String = ""
            err = clsCommand.SQL_cmd(sql)
            If err = "" Then
                Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "EditSectionProfile.aspx")
            Else
                img.Visible = True
                lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
            End If
        End If
    End Sub

    Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        Try
            Select Case e.Row.RowType
                Case DataControlRowType.DataRow
                    Dim imgBD As ImageButton = e.Row.FindControl("imgBD")
                    Dim imgBH As ImageButton = e.Row.FindControl("imgBH")
                    Dim lblstatusid As Label = e.Row.FindControl("lblstatusid")
                    If Not IsNothing(imgBD) Then
                        If lblstatusid.Text = "2" Then
                            imgBH.Visible = False
                        Else
                            imgBH.Visible = True
                        End If
                        If lblstatusid.Text = "3" Then
                            imgBD.Visible = False
                        Else
                            imgBD.Visible = True
                        End If
                    End If
            End Select
        Catch ex As Exception

        End Try
    End Sub
End Class
